﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using ZK.Models;

namespace ZK.HardWareBLL
{
    /// <summary>
    /// 煤管票bll
    /// </summary>
    public class CMT
    {

        string CMTInIp;
        string CMTOutIp;

        public CMT()
        {
            CMTInIp = Common.AppConfigHelper.GetAppSettings("CMTInIp");
            CMTOutIp = Common.AppConfigHelper.GetAppSettings("CMTOutIp");
        }

        private static CMT cMT;
        public static CMT GetInstance()
        {
            if (cMT == null)
            {
                cMT = new CMT();
            }
            return cMT;
        }

        public void Send(TicketSendM ticket)
        {
            try
            {
                string ip = this.CMTInIp;
                if (ticket.IsTicket == "是")
                {
                    ip = this.CMTOutIp;
                }
                StringBuilder builder = new StringBuilder();
                IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ip), 0x2014);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(remoteEP);
                switch (ticket.IsTicket)
                {
                    case "是":
                        builder = new StringBuilder();
                        builder.Append(ticket.CoalTypeName ?? "");
                        builder.Append("\t");
                        builder.Append(ticket.CarNumber.ToUpper() ?? "");
                        builder.Append("\t\t");
                        builder.Append(ticket.CarType ?? "");
                        builder.Append("\t");
                        builder.Append(ticket.Driver ?? "");
                        builder.Append("\t");
                        builder.Append(ticket.CustomerName ?? "");
                        builder.Append("\t\t\t\t\t\t\t\t\t");
                        builder.Append(ticket.GrossWeight ?? "");
                        builder.Append("\t");
                        builder.Append(ticket.Tare ?? "");
                        builder.Append("\t");
                        builder.Append(ticket.Province ?? "");
                        builder.Append("\t");
                        builder.Append(ticket.City ?? "");
                        builder.Append("\t\t\t\t\t\t\t\t\t\t\t");
                        break;

                    case "否":   //境内票
                        if (!string.IsNullOrEmpty(ticket.CustomerName) && ((ticket.CustomerName.Length >= 0x12) && (ticket.CustomerName.Length <= 0x17)))
                        {
                            builder = new StringBuilder();
                            builder.Append(ticket.CustomerName ?? "");
                            builder.Append("\t\t");
                            builder.Append(ticket.CarNumber.ToUpper() ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.CarType ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.Driver ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.GrossWeight ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.Tare ?? "");
                            builder.Append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
                        }
                        else if (!string.IsNullOrEmpty(ticket.CustomerName) && (ticket.CustomerName.Length == 0x18))
                        {
                            builder = new StringBuilder();
                            builder.Append("\b\b");
                            builder.Append(ticket.CustomerName ?? "");
                            builder.Append("\t\t");
                            builder.Append(ticket.CarNumber.ToUpper() ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.CarType ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.Driver ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.GrossWeight ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.Tare ?? "");
                            builder.Append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
                        }
                        else if (!string.IsNullOrEmpty(ticket.CustomerName) && ((ticket.CustomerName.Length >= 0x19) && (ticket.CustomerName.Length <= 0x1c)))
                        {
                            builder = new StringBuilder();
                            builder.Append(ticket.CustomerName ?? "");
                            builder.Append("\t\t");
                            builder.Append(ticket.CarNumber.ToUpper() ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.CarType ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.Driver ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.GrossWeight ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.Tare ?? "");
                            builder.Append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
                        }
                        else
                        {
                            builder = new StringBuilder();
                            builder.Append("\b\b\b\b\b\b\b\b\b\b\b\b");
                            builder.Append(ticket.CustomerName ?? "");
                            builder.Append("\t\t");
                            builder.Append(ticket.CarNumber.ToUpper() ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.CarType ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.Driver ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.GrossWeight ?? "");
                            builder.Append("\t");
                            builder.Append(ticket.Tare ?? "");
                            builder.Append("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
                        }
                        break;
                }
                byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(builder.ToString());
                socket.Send(bytes);
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
                //LogHelper.LogInfo("TransferEx发送数据为：" + builder.Replace('\b', 'b').Replace("b", @"\b").Replace('\t', 't').Replace("t", @"\t").ToString());
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
    }
}
